package com.tanhua.server.controller;

import com.tanhua.common.vo.ErrorResult;
import com.tanhua.server.service.IMService;
import com.tanhua.server.vo.PageResult;
import com.tanhua.server.vo.UserInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RequestMapping("messages")
@RestController
@Slf4j
public class IMController {

    @Autowired
    private IMService imService;

    /**
     * 根据环信用户名查询用户信息
     *
     * @param userName 环信用户
     * @return
     */
    @GetMapping("userinfo")
    public UserInfoVo queryUserInfoByUserName(@RequestParam("huanxinId") String userName) {
        return this.imService.queryUserInfoByUserName(userName);
    }

    /**
     * 添加好友
     *
     * @param param
     * @return
     */
    @PostMapping("contacts")
    public Object contactUser(@RequestBody Map<String, Object> param) {
        Long friendId = Long.valueOf(param.get("userId").toString());
        boolean result = this.imService.contactUser(friendId);
        if(result){
            return null;
        }
        return ErrorResult.builder().errCode("500").errMessage("好友关系已经存在！").build();
    }

    /**
     * 查询联系人列表
     *
     * @param page
     * @param pageSize
     * @param keyword
     * @return
     */
    @GetMapping("contacts")
    public PageResult queryContactsList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                        @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize,
                                        @RequestParam(value = "keyword", required = false) String keyword) {
        return this.imService.queryContactsList(page, pageSize, keyword);
    }

}
